/***************************************************************************
 *   Copyright (C) 2008 by Chris Bornholdt,,,   *
 *   chris.bornholdt@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef _STRIDE_ITERATOR_HPP_
#define _STRIDE_ITERATOR_HPP_

#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/concept_check.hpp>
#include <boost/utility/enable_if.hpp>

template<typename Base>
class stride_iterator : public boost::iterator_adaptor<stride_iterator<Base>, Base,
			boost::use_default, std::random_access_iterator_tag>
{
	BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Base>));
	
	struct enabler {};
	friend class boost::iterator_core_access;
	
	public:
		typedef typename stride_iterator::iterator_adaptor_::difference_type difference_type;
	
		stride_iterator() : stride_iterator::iterator_adaptor_(), stride_(1) {}
		explicit stride_iterator(const Base& base)
			: stride_iterator::iterator_adaptor_(base), stride_(1) {}
		stride_iterator(const Base& base, unsigned int stride)
			: stride_iterator::iterator_adaptor_(base), stride_(stride) {}
		
		template<typename OtherBase>
		stride_iterator(const stride_iterator<OtherBase>& other, 
			typename boost::enable_if<
			typename boost::is_convertible<OtherBase, Base>::type, enabler
			>::type = enabler())
			: stride_iterator::iterator_adaptor_(other.base()), stride_(other.stride_) {}
			
		unsigned int stride() const
		{	return stride_;	}
		
	private:
		void increment()	{	this->base_reference() += static_cast<difference_type>(stride_);	}
		void decrement()	{	this->base_reference() -= static_cast<difference_type>(stride_);	}
		void advance(int n)
		{	this->base_reference() += n * static_cast<difference_type>(stride_);	}
		
		int distance_to(const stride_iterator& other) const
		{	return (other.base() - this->base()) / static_cast<int>(stride_);	}
		
		template<typename Other>
		difference_type distance_to(const Other& other) const
		{	return (other.base() - this->base()) / static_cast<difference_type>(stride_);	}
	
		unsigned int stride_;
};

#endif
